package com.dy.字符串.验证回文字符串;

public class Solution {
    public static boolean isPalindrome(String s) {
        if(s.equals("")){
            return true;
        }
        s=s.toLowerCase();
        char a[] = new char[s.length()];
        int j=0;
        for(int i =0;i<s.length();i++){
            if(('a'<=s.charAt(i) && s.charAt(i)<='z') || ('0'<=s.charAt(i) && s.charAt(i)<='9')){
                a[j++]  = s.charAt(i);
            }
        }
        int start = 0;
        int end = j-1;
        while(start<end){
            if(a[start++]!=a[end--]){
                return false;
            }
        }
        return true;
    }
//不用先生成字符串，直接比较
    public static boolean isPalindrome2(String s) {
        if(s.equals("")){
            return true;
        }
        s=s.toLowerCase();
        int start=0;
        int end = s.length()-1;
        while(start<end) {
            char head = s.charAt(start);
            char tail = s.charAt(end);
            if(!(('a'<=head && head<='z') || ('0'<=head && head<='9'))){
                start++;
                continue;
            }
            if(!(('a'<=tail && tail<='z') || ('0'<=tail && tail<='9'))){
                end--;
                continue;
            }
            if(head==tail){
                start++;
                end--;
            }
            else return false;
        }
        return true;
    }
}
